package cup.cqzj.auth.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import cup.common.common.MyRoleConstant;
import cup.common.config.CqjgptPointConfiguration;
import cup.core.launch.constant.AppConstant;
import cup.core.log.annotation.ApiLog;
import cup.core.secure.annotation.PreAuth;
import cup.core.tenant.annotation.NonDS;
import cup.core.tool.api.R;
import cup.core.tool.constant.RoleConstant;
import cup.core.tool.support.Kv;
import cup.core.tool.utils.Func;
import cup.cqzj.auth.dto.DsfUserDTO;
import cup.cqzj.auth.service.DsfTokenGranter;
import cup.cqzj.auth.service.TokenGranterUtil;
import cup.cqzj.auth.vo.CheckCodeVo;
import cup.modules.auth.utils.TokenUtil;
import cup.modules.system.entity.Role;
import cup.modules.system.entity.User;
import cup.modules.system.entity.UserInfo;
import cup.modules.system.service.IDeptService;
import cup.modules.system.service.IPostService;
import cup.modules.system.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : ruoyu
 * @date : 2022/8/9
 */
@NonDS
@ApiSort(1)
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_AUTH_NAME + "/cqjgpt")
@Api(value = "一网通办登录", tags = "一网通办登录")
@Slf4j
public class CqjgptTokenEndPoint {

	private final DsfTokenGranter dsfTokenGranter;
	private final IRoleService roleService;
	private final TokenGranterUtil tokenGranterUtil;

	@ApiLog("验证用户登录后code信息")
	@PostMapping("/check")
	@ApiOperation(value = "验证用户登录后code信息", notes = "验证用户登录后code信息")
	@ApiOperationSupport(order = 1)
	public R<CheckCodeVo> checkCode(String code) {
		log.info("接收到第三方登录请求++++++++++++++++++++++");
		DsfUserDTO userInfo = dsfTokenGranter.getUserInfo(code);
		CheckCodeVo checkCodeVo = dsfTokenGranter.checkUser(userInfo);
		return R.data(checkCodeVo);
	}

	@ApiLog("第三方登录获取token")
	@PostMapping("/token")
	@ApiOperation(value = "status=1 获取登录token", notes = "status=1 获取登录token")
	@ApiOperationSupport(order = 2)
	public Kv token(String code) {
		//获取user信息
		User user = dsfTokenGranter.getUserEntity(code);
		UserInfo userInfo = new UserInfo();
		userInfo.setUser(user);
		List<Role> roles = roleService.listByIds(Func.toLongList(user.getRoleId()));
		if (!CollectionUtils.isEmpty(roles)) {
			userInfo.setRoles(roles.stream().map(Role::getRoleAlias).collect(Collectors.toList()));
		}
		dsfTokenGranter.removeCacheUserInfo(code);
		tokenGranterUtil.generateDeptAndPostName(userInfo);
		return TokenUtil.createAuthInfo(userInfo);
	}


	@ApiLog("绑定系统已有账户")
	@PostMapping("/bond-exist")
	@ApiOperation(value = "绑定系统已有账户", notes = "绑定系统已有账户")
	@ApiOperationSupport(order = 3)
	public R bondExist(String code, String account, String password) {
		dsfTokenGranter.bondExist(code, account, password);
		return R.success("绑定成功,请联系后台管理员激活该账号再次一网通登录!");
	}

	@ApiLog("新增并绑定用户")
	@PostMapping("/bond-add")
	@ApiOperation(value = "新增并绑定用户", notes = "新增并绑定用户")
	@ApiOperationSupport(order = 4)
	public R bondAdd(String code, String account, String password) {
		dsfTokenGranter.bondAdd(code, account, password);
		return R.success("绑定成功,请联系后台管理员激活该账号再次一网通登录!");
	}
}
